// pages/certificate/certificate.js
var dmNetwork = require('../../utils/network.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    frontCardImg: '',
    backCardImg: '',
    uploadFrontImg: '',
    uploadBackImg: '',
    name: '',
    idCard: '',
    mobile: '',
    isAuth: false,
    agree: false
  },
  uploadImg(e) {
    if (this.data.isAuth) return
    const that = this
    const target = e.currentTarget.dataset.img
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths[0]
        if (target === 'frontCardImg') {
          that.setData({
            frontCardImg: tempFilePaths
          })
          wx.uploadFile({
            url: "https://image-inner.doumi.com/upload.php",
            filePath: tempFilePaths,
            name: `uploadfile_ant`,
            formData: {},
            header: {
              "Content-Type": "multipart/form-data",
            },
            success(res) {
              const uploadResData = JSON.parse(res.data)
              that.setData({
                uploadFrontImg: uploadResData.info[0].url
              })
            },
            fail() {
              wx.hideLoading()
              wx.showToast({
                title: '上传失败，请重新上传',
                icon: 'none'
              })
            }
          })
        } else {
          that.setData({
            backCardImg: tempFilePaths
          })
          wx.uploadFile({
            url: "https://image-inner.doumi.com/upload.php",
            filePath: tempFilePaths,
            name: `uploadfile_ant`,
            formData: {},
            header: {
              "Content-Type": "multipart/form-data",
            },
            success(res) {
              const uploadResData = JSON.parse(res.data)
              that.setData({
                uploadBackImg: uploadResData.info[0].url
              })
            },
            fail() {
              wx.hideLoading()
              wx.showToast({
                title: '上传失败，请重新上传',
                icon: 'none'
              })
            }
          })
        }

      }
    })
  },
  bindInputName(e) {
    this.setData({
      name: e.detail.value
    })
    console.log(this.data.name);
  },
  bindInputIdCard(e) {
    this.setData({
      idCard: e.detail.value
    })
    console.log(this.data.idCard);
  },
  changeAgree() {
    this.setData({
      agree: !this.data.agree
    })
  },
  auth() {
    let that = this
    if (!this.data.agree) {
      wx.showToast({
        title: '请勾选条款',
        icon: 'none'
      })
      return
    }
    if (!this.data.frontCardImg) {
      wx.showToast({
        title: '请上传身份证正面照',
        icon: 'none'
      })
      return
    }
    if (!this.data.backCardImg) {
      wx.showToast({
        title: '请上传身份证反面照',
        icon: 'none'
      })
      return
    }
    if (!this.data.name || this.data.name.length < 2) {
      wx.showToast({
        title: '请输入正确的姓名',
        icon: 'none'
      })
      return
    }
    if (!/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(this.data.idCard)) {
      wx.showToast({
        title: '请输入18位的有效身份证',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '认证中',
      mask: true,
    })
    let params = {
      name: that.data.name,
      idnumber: that.data.idCard,
      card_front: that.data.uploadFrontImg,
      card_back: that.data.uploadBackImg
    }
    dmNetwork.post(dmNetwork.auth, params, (res) => {
      if (res.data.errno == 0) {
        wx.hideLoading()
        that.setData({
          isAuth: true
        })
        wx.showToast({
          title: '认证成功',
          icon: 'none',
          duration: 2000,
        })
      } else {
        wx.showToast({
          title: res.data.errmsg,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      isAuth: options.isAuth === '1' ? true : false
    })
    // if (this.data.isAuth) {
    this.getInfo()
    // }

  },
  getInfo() {
    dmNetwork.get(dmNetwork.get_auth_info, {}, (res) => {
      if (res.data.errno == 0) {
        this.setData({
          frontCardImg: res.data.data.card_front,
          backCardImg: res.data.data.card_back,
          mobile: res.data.data.mobile,
          name: res.data.data.real_name,
          idCard: res.data.data.idnumber
        })
      }
    })
  },
  goClause() {
    wx.navigateTo({
      url: '../secret_text/secret_text'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})